package com.example.servlet.flight;

import com.example.entity.City;
import com.example.entity.FlightDetail;
import com.example.entity.User;
import com.example.service.FlightService;
import com.example.service.impl.FlightServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/user_flight_search3_3")
public class uFlightSearch33Servlet extends HttpServlet {
    private FlightService service;
    private SqlSession sqlSession;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new FlightServiceImpl(sqlSession);
    }
    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }

    private void processArrayValues(String[] values, String variablePrefix, Context context) {
        if (values != null) {
            for (int i = 0; i < values.length && i < 5; i++) {
                String variableName = variablePrefix + (i + 1);
                context.setVariable(variableName, values[i]);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        String[] departureCities = req.getParameterValues("multiDepartureCities[]");
        String[] arrivalCities = req.getParameterValues("multiArrivalCities[]");
        String[] departureDates = req.getParameterValues("multiDepartureDates[]");
        Date departureDate = null;
        try {
            if(departureDates[2] != null){
                departureDate = new SimpleDateFormat("yyyy-MM-dd").parse(departureDates[0]);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Context context = new Context();
        List<FlightDetail> flightDetails = new ArrayList<>();
        if (service.isCityExist(departureCities[2]) == 0 && service.isCityExist(arrivalCities[2]) == 0) {
            flightDetails.addAll(service.searchFlightDetails(departureCities[2], arrivalCities[2], departureDate));
        } else if (service.isCityExist(departureCities[2]) == 0 && service.isCityExist(arrivalCities[2]) !=0 ) {
            context.setVariable("no_arrival", true);
            City city = service.getNearCity(arrivalCities[2]);
            String arrivalCity1 = city.getCity1();
            String arrivalCity2 = city.getCity2();
            flightDetails.addAll(service.searchFlightDetails(departureCities[2], arrivalCity1, departureDate));
            flightDetails.addAll(service.searchFlightDetails(departureCities[2], arrivalCity2, departureDate));
        } else if (service.isCityExist(departureCities[2]) != 0 && service.isCityExist(arrivalCities[2]) == 0) {
            context.setVariable("no_departure", true);
            City city = service.getNearCity(departureCities[2]);
            String departureCity1 = city.getCity1();
            String departureCity2 = city.getCity2();
            flightDetails.addAll(service.searchFlightDetails(departureCity1, arrivalCities[2], departureDate));
            flightDetails.addAll(service.searchFlightDetails(departureCity2, arrivalCities[2], departureDate));
        } else{
            context.setVariable("no_departure_arrival", true);
            City city1 = service.getNearCity(departureCities[2]);
            City city2 = service.getNearCity(arrivalCities[2]);
            flightDetails.addAll(service.searchFlightDetails(city1.getCity1(), city2.getCity1(), departureDate));
            flightDetails.addAll(service.searchFlightDetails(city1.getCity1(), city2.getCity2(), departureDate));
            flightDetails.addAll(service.searchFlightDetails(city1.getCity2(), city2.getCity1(), departureDate));
            flightDetails.addAll(service.searchFlightDetails(city1.getCity2(), city2.getCity2(), departureDate));
        }
        context.setVariable("user", user);
        context.setVariable("flightdetails", flightDetails);
        processArrayValues(departureCities, "departureCity", context);
        processArrayValues(arrivalCities, "arrivalCity", context);
        processArrayValues(departureDates, "departureDate", context);
        context.setVariable("tripCount", departureCities.length);
        context.setVariable("departureCities", departureCities);
        context.setVariable("arrivalCities", arrivalCities);
        context.setVariable("departureDates", departureDates);
        // 图片是否存在判断
        String imagePath = getServletContext().getRealPath("/static/images/" + user.getU_id() + ".jpg");
        File imageFile = new File(imagePath);
        boolean imageExists = imageFile.exists();
        context.setVariable("imageExists", imageExists);
        ThymeleafUtil.process("user_flight_result3_3.html", context, resp.getWriter());
    }
}
